package oa.hleast.xswl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.utils.JsonUtil;
import oa.hleast.xswl.common.constant.ConfigKeyConstant;
import oa.hleast.xswl.common.enums.AppTypeKeyEnum;
import oa.hleast.xswl.pojo.domain.CmsVenueMain;
import oa.hleast.xswl.pojo.domain.SysDictItem;
import oa.hleast.xswl.pojo.vo.activity.ShowCommonActivityVo;
import oa.hleast.xswl.pojo.vo.appsicon.HomePageAppsIconVo;
import oa.hleast.xswl.pojo.vo.article.HomePageArticleVo;
import oa.hleast.xswl.pojo.vo.article.HomePageData;
import oa.hleast.xswl.pojo.vo.article.ShowArticleVo;
import oa.hleast.xswl.pojo.vo.place.CmsPlaceTypeVo;
import oa.hleast.xswl.pojo.vo.place.ShowPlaceInfoVo;
import oa.hleast.xswl.pojo.vo.pms.ShowSpuVo;
import oa.hleast.xswl.pojo.vo.web.*;
import oa.hleast.xswl.service.ISysCommonConfigService;
import oa.hleast.xswl.service.ISysDictItemService;
import oa.hleast.xswl.service.IWebService;
import oa.hleast.xswl.service.activity.ICmsActivityCalendarService;
import oa.hleast.xswl.service.activity.ICmsActivityTemplateService;
import oa.hleast.xswl.service.activity.ICmsCommonActivityService;
import oa.hleast.xswl.service.activity.ICmsUserSignUpService;
import oa.hleast.xswl.service.appsicon.IAppsIconService;
import oa.hleast.xswl.service.article.IInformationCentreService;
import oa.hleast.xswl.service.banner.IWebsiteBannerService;
import oa.hleast.xswl.service.place.IPlaceInfoService;
import oa.hleast.xswl.service.place.IPlaceTypeService;
import oa.hleast.xswl.service.pms.IPmsSpuService;
import oa.hleast.xswl.service.venue.CmsVenueMainService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@AllArgsConstructor
@Service
@Transactional
public class WebServiceImpl implements IWebService {

    private final IWebsiteBannerService websiteBannerService;
    private final ISysCommonConfigService commonConfigService;
    private final IAppsIconService appsIconService;
    private final IPlaceInfoService placeInfoService;
    private final ISysDictItemService dictItemService;
    private final IInformationCentreService informationCentreService;
    private final IPlaceTypeService placeTypeService;
    private final IPmsSpuService pmsSpuService;


    private final ICmsCommonActivityService commonActivityService;
    private final ICmsActivityCalendarService calendarService;
    private final ICmsActivityTemplateService templateService;
    private final ICmsUserSignUpService signUpService;
    private final IPmsSpuService iPmsSpuService;
    private final CmsVenueMainService cmsVenueMainService;

    @Override
    public HomePageServeDataVo getCityBrainServeData() {
        String configKey = ConfigKeyConstant.CITY_BRAIN;
        Page<HomePageWebsiteBannerVo> pageWebsiteBannerVoPage = websiteBannerService.getWebsiteBannerPageByKey(1, 100, configKey);
        HomePageServeDataVo vo = HomePageServeDataVo.builder()
                .websiteBannerVo(pageWebsiteBannerVoPage.getRecords())
                .build();
        return vo;
    }

    @Override
    public HomePageRedTourismServeDataVo getRedTourismServeData() {
        //轮播图
        Page<HomePageWebsiteBannerVo> banner = websiteBannerService.getWebsiteBannerPageByKey(1, 100, "red_home");
        // 研学路线
        List<CmsPlaceTypeVo> researchInfo = placeTypeService.finsShowPlaceTypeOnlyLast(1L);
       // 云展馆
        List<ShowPlaceInfoVo> cloudShowInfo = placeInfoService.findShowPlaceInfo(8L, null, null);

        // 应用图标
        Long dictItemAppsIconTypeId = dictItemService.getDictItemByConfigKey("application_red_home").getId();
        Page<HomePageAppsIconVo> appsIcon = appsIconService.getHomePageIcon(1, 100, dictItemAppsIconTypeId);

//        String marketHomeDate = commonConfigService.getByConfigKey("market_home_date").getValue();
//        JsonUtil.string2Obj(marketHomeDate,List<Map<String,Object>>)
        // 云集市
//        List<Map<String, Object>> cloudMarket = JsonUtil.string2Collection(marketHomeDate, new TypeReference<List<Map<String, Object>>>(){});
        Page<ShowSpuVo> cloudMarketPage = iPmsSpuService.recommendList(1,4);
        // 场地预约
        Page<CmsVenueMain> venueMainList = cmsVenueMainService.findVenueVoList(1, 3, null, null);


        // 新闻咨询
        Page<ShowCommonActivityVo> showCommonActivityVoList = commonActivityService.findShowActivity(1, 4, 3L);

        HomePageRedTourismServeDataVo vo = HomePageRedTourismServeDataVo.builder()
                .websiteBanner(banner.getRecords())
                .researchInfo(researchInfo)
                .cloudShowInfo(cloudShowInfo)
                .cloudMarket(cloudMarketPage.getRecords())
                .appIcon(appsIcon.getRecords())
                .venueMainVoList(venueMainList.getRecords())
                .showArticleVoList(showCommonActivityVoList.getRecords())
                .build();
        return vo;
    }

    @Override
    public HomePageArticleVo getCloudClassroomServeData() {
        HomePageArticleVo vo = new HomePageArticleVo();
        List<HomePageData> dataList = new ArrayList<>();
        String ids = commonConfigService.getByConfigKey("cloud_classroom").getValue();
        log.info("ids = {}", ids);
        for (String placeTypeId : Arrays.asList(ids.split(","))) {
            HomePageData homePageData = new HomePageData();
            SysDictItem dictItem = dictItemService.getById(placeTypeId);
            log.info("dictItem = {}", dictItem);
            Page<ShowArticleVo> showArticleVos = informationCentreService.findPageShowList(1, 100, placeTypeId);
            homePageData.setTitle(dictItem.getName());
            homePageData.setArticleVoList(showArticleVos.getRecords());
            dataList.add(homePageData);
        }
        vo.setDataList(dataList);
        return vo;
    }

    @Override
    public HomePageArticleVo getResearchServeData() {
        HomePageArticleVo vo = new HomePageArticleVo();
        List<HomePageData> dataList = new ArrayList<>();
        String ids = commonConfigService.getByConfigKey("research_home_date").getValue();
        log.info("ids = {}", ids);
        for (String placeTypeId : Arrays.asList(ids.split(","))) {
            HomePageData homePageData = new HomePageData();
            SysDictItem dictItem = dictItemService.getById(placeTypeId);
            log.info("dictItem = {}", dictItem);
            Page<ShowArticleVo> showArticleVos = informationCentreService.findPageShowList(1, 100, placeTypeId);
            homePageData.setTitle(dictItem.getName());
            homePageData.setArticleVoList(showArticleVos.getRecords());
            dataList.add(homePageData);
        }
        vo.setDataList(dataList);
        return vo;
    }

    @Override
    public HomePageCloudMarketServeDataVo getCloudMarketServeData() {

        Page<HomePageWebsiteBannerVo> banner = websiteBannerService.getWebsiteBannerPageByKey(1, 100, "banner_bazaar");
        Long dictItemAppsIconTypeId = dictItemService.getDictItemByConfigKey("red_bazaar").getId();
        Page<HomePageAppsIconVo> appsIcon = appsIconService.getHomePageIcon(1, 100, dictItemAppsIconTypeId);

        String marketHomeDate = commonConfigService.getByConfigKey("product_data").getValue();
//        JsonUtil.string2Obj(marketHomeDate,List<Map<String,Object>>)
        List<Map<String, Object>> productData = JsonUtil.string2Collection(marketHomeDate, new TypeReference<List<Map<String, Object>>>(){});

        HomePageCloudMarketServeDataVo vo = HomePageCloudMarketServeDataVo.builder()
                .websiteBanner(banner.getRecords())
                .appIcon(appsIcon.getRecords())
                .productList(productData)
                .build();
        return vo;
    }

    @Override
    public HomePageMallDataVo getMallData() {
        Long dictItemPlayId = new Long(commonConfigService.getByConfigKey("red_bazaar").getValue());
        List<HomePageAppsIconVo> playAppsIconVo = appsIconService.getAppHomePageIcon(1, 10, dictItemPlayId).getRecords();
        return HomePageMallDataVo.builder()
                .appsIconVo(playAppsIconVo)
                .websiteBannerVo(websiteBannerService.getHomePageMallWebsiteBanner(1, 10).getRecords())
                .spuList(pmsSpuService.recommendList(1, 10).getRecords())
                .build();
    }

}
